<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    // es5写法
    function add(a,b){
        a = a || 10;
        b = b || 20;
        return a + b;
    }
    console.log(add());

    // 1. es6写法
    function add2(a = 10,b = 20){
        return a + b;
    }
    console.log(add2());

    // 2. 默认值可以是函数

    function getVal(){
        return 30;
    }
    function add3(a,b = getVal()){
        return a + b;
    }
    console.log(add3(2));

    // 剩余参数es5 写法
    function pick(obj){
        let result = Object.create(null);
        for (let i = 1;i < arguments.length;i++){
            result[arguments[i]] = obj[arguments[i]];
        }
        return result;
    }
    const book = {
        name:'es6教程-es5',
        author:'sf',
        date:'2025-4-10'
    }
    console.log(pick(book,'name'));
    console.log(pick(book,'name','author'));
    console.log(pick(book,'date'));

    // 剩余参数es6写法
    function pick2(obj , ...params){
        let result = Object.create(null);
        for (let i = 0;i < params.length;i++){
            result[params[i]] = obj[params[i]];
        }
        return result;
    }
    const book2 = {
        name:'es6教程-es6',
        author:'sf',
        date:'2025-4-10'
    }
    console.log(pick2(book2,'name'));
    console.log(pick2(book2,'name','author'));
    console.log(pick2(book2,'date'));

    function checkArgs(...args){
        console.log(args);
        console.log(arguments);
    }
    checkArgs('a','b','c');
</script>
</body>
</html>